package uk.co.accolade.clash.core.message;

public class DataToken {

    private static final String SEPARATOR = ":";

    String key;
    String value;

    public DataToken( String key, String value ) {
        this.key = key;
        this.value = value;
    }

    static DataToken[] tokensFromMessage( String message ) {
        String[] tokens = extractTokensFrom( message );
        DataToken[] dataTokens = new DataToken[tokens.length / 2];
        for( int i = 0; i < tokens.length; i = i+2 ) {
            dataTokens[i/2] = new DataToken( tokens[i], tokens[i+1] );
        }
        return dataTokens;
    }

    private static String[] extractTokensFrom( String message ) {
        String[] tokens = message.split( SEPARATOR );
        if( !(tokens.length % 2 == 0) ) {
            throw new ClashEventParseException( "Clash message [" + message + "] is badly formatted." );
        }
        return tokens;
    }

}
